//
//  CinemaViewController.m
//  WXMovie
//
//  Created by apple on 16/3/29.
//  Copyright © 2016年 zic_magic. All rights reserved.
//

#import "CinemaViewController.h"
#import "CinemaModel.h"
#import "CinemaTableViewCell.h"
#import "SectionHeaderView.h"
@interface CinemaViewController ()
{
    NSMutableDictionary *_modelDic;
    NSArray *districtSubjects;
    
    BOOL distrisctSelect[30];
    
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@end

@implementation CinemaViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self _loadData];
    [self _createView];
    
    
}

- (void)_loadData {
    
    _modelDic = [NSMutableDictionary dictionary];
    
    NSData *districtData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"district_list" ofType:@"json"] ];
    
    NSData *cinemaData = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"cinema_list" ofType:@"json"]];
    
    id districtDataResult = [NSJSONSerialization JSONObjectWithData:districtData options:NSJSONReadingMutableContainers error:nil];
    id cinemaDataResult = [NSJSONSerialization JSONObjectWithData:cinemaData options:NSJSONReadingMutableContainers error:nil];
    
    districtSubjects = districtDataResult[@"districtList"];
    NSArray *cinemaSubjects = cinemaDataResult[@"cinemaList"];
    
    for (NSInteger i = 0; i < districtSubjects.count; i++) {
        
        NSMutableArray *array = [[NSMutableArray alloc] init];
        
        for (NSInteger j = 0; j < cinemaSubjects.count; j++) {
            NSString *districtStr = districtSubjects[i][@"id"];
            NSString *addressStr = cinemaSubjects[j][@"districtId"];
           
            if ([districtStr isEqualToString:addressStr]) {
                CinemaModel *model = [[CinemaModel alloc] init];
                
                
                model.lowPrice = cinemaSubjects[j][@"lowPrice"];
                model.grade = cinemaSubjects[j][@"grade"];
                model.address = cinemaSubjects[j][@"address"];
                model.name = cinemaSubjects[j][@"name"];
                model.lowPrice = cinemaSubjects[j][@"lowPrice"];
                if ([model.lowPrice isKindOfClass:[NSNull class]]) {
                    model.lowPrice = @"";
                }
                              
//                NSLog(@"%@", cinemaSubjects[j][@"address"]);

                [array addObject:model];
            }
        }
        [_modelDic setValue:array forKey:districtSubjects[i][@"name"]];
        array = nil;
        
    }
//    NSLog(@"%@", _modelDic);
    
    
}

- (void)_createView {
    
    [self.tableView registerNib:[UINib nibWithNibName:@"CinemaTableViewCell" bundle:nil] forCellReuseIdentifier:@"CinemaTableViewCell"];
    self.tableView.rowHeight = kScreenHeight / 8;
    self.tableView.backgroundColor = [UIColor clearColor];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    if (!distrisctSelect[section]) {
        return 0;
    }
    NSArray *cinemaArr = [_modelDic objectForKey:districtSubjects[section][@"name"]];
//    NSLog(@"%li", cinemaArr.count);
    return cinemaArr.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    
    return districtSubjects.count;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    
    
    SectionHeaderView *headerView = [tableView dequeueReusableHeaderFooterViewWithIdentifier:@"cinemaHeader"];
    if (headerView == nil) {
        headerView = [[SectionHeaderView alloc] initWithReuseIdentifier:@"cinemaHeader"];
        
        [headerView.ctrlView addTarget:self action:@selector(clickSectionHeader:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    headerView.ctrlView.tag = 1000 + section;
    
    headerView.titleLabel.text = districtSubjects[section][@"name"];
    
    return headerView;
}
-(void)clickSectionHeader:(UIControl *)headView
{
    NSInteger section = headView.tag - 1000;
    distrisctSelect[section] = !distrisctSelect[section];
    
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:section] withRowAnimation:UITableViewRowAnimationFade];
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    CinemaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CinemaTableViewCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor clearColor];

    NSArray *modelArray = _modelDic[districtSubjects[indexPath.section][@"name"]];
//    NSLog(@"%@", modelArray);
    cell.model = modelArray[indexPath.row];
    
    
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 35;
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
